package com.suitsoft.ut.dao.account;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.hibernate.HibernateDao;

import com.suitsoft.ut.entity.system.STreeInfo;
import com.suitsoft.ut.entity.system.SystemInfo;

/**
 * 对系统参数的操作.
 * 
 * @author kxx
 */
@Repository
public class SystemInfoDao extends HibernateDao<SystemInfo, Long> {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 查找指定类别下的所有系统参数信息.
	 */
	public List<SystemInfo> getSystemInfoBySystemCategory(Long scId) {
		String hql = "from SystemInfo where systemCategory.id = ? order by to_number(pmcd)";
		return find(hql, scId);
	}

	public STreeInfo findtree(Long id) {
		return (STreeInfo) getSession().get(STreeInfo.class, id);
	}

	public void updatetree(STreeInfo po) {
		getSession().saveOrUpdate(po);
	}
	/**
	 * 取得systemInfo实体
	 * @return
	 */
	public SystemInfo getSystemInfo() {
		String sql = "SELECT T.SYSTEM_KEY_CODE FROM HR_SYSTEM_INFO T WHERE T.PCNM = '住宅'";
		Long id = null;

		try {
			id = jdbcTemplate.queryForLong(sql);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		return this.get(id);
	}
	/**根据pmcd取得主键*/
	public Long getSystemCode(String pmcd, String pmky){
		StringBuffer buffer= new StringBuffer();
		buffer.append("select t.system_key_code from hr_system_info t");
		buffer.append(" where t.category_key_code =(select s.category_key_code from hr_system_category s where pmky='");
		buffer.append(pmky);
		buffer.append("')");
		buffer.append(" and t.pmcd='");
		buffer.append(pmcd);
		buffer.append("'");
		String sql= buffer.toString();
		return jdbcTemplate.queryForLong(sql);
	}
	/**
	 * 根据pmky取得pcnm
	 * */
	
	public List<Map<String, Object>> comCertity(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("select s.pcnm ,s.system_key_code from hr_system_info s , hr_system_category sc ");
		buffer.append(" where s.category_key_code = sc.category_key_code and sc.pmky='com_certify'");
		
		String sql = buffer.toString();
		return jdbcTemplate.queryForList(sql);
		
	}
	
	public void update(Object obj){
		getSession().update(obj);
	}
}
